﻿<?php
//节目
class CategoryAction extends CommonAction {

    function index()
	{
		$name = $this->getActionName();
		$m = M($name); // 实例化Data数据对象
		import('ORG.Util.Page');// 导入分页类
		$map = array('status'=>1);
		$count      = $m->where($map)->count();
		$Page       = new Page($count,10);
		$Page->rollPage = 20;	//最多显示20个分页

		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数, 使用 $_GET[p]获取
		$nowPage = isset($_REQUEST['p']) ? $_REQUEST['p'] : 1;
		$offect = ($nowPage-1)*$Page->listRows;

		$sql = 'select cl.id, cl.name_en, cl.name,cl.status  from '.getTableName($name).' as cl left join '.getTableName($name).' as cr on cl.pid=cr.id
				order by cl.pid
				limit '.$offect.','.$Page->listRows;
        

		$list = $m->query( $sql );

		$show = $Page->show();
		$this->assign('page',$show);
		$this->assign('list',$list);
		$this->display();
	}

	public function edit() {
        $name='Category';
        $m = M($name);
        if($_POST['subflag']){
            $m->create();
            $result = $m->save();

            if($result) {
                $this->success('操作成功！',"index");
            }else{
                $this->error('写入错误！',"index");
            }
           
        }else{ 
            if ($this->_get('id')) {
                $condition['id'] = $this->_get('id');
                $res = $m->where($condition)->select();
                if($res){
                    $sql = 'select a1.id,a1.name,a2.name pname from '.getTableName($name).' a1 left join '.getTableName($name).' a2 on a1.pid=a2.id order by a1.pid';
                    $pcategory = $m->query($sql);
                    $this->assign('pcategory', $pcategory); 
                    $this->assign('vo', $res[0]); 
                }
            }else{
                $this->ajaxReturn(0, '非法进入', 0);
            }
        }

        $this->display();
    }
    
    
    
    public function add(){
        $name='Category';
        $m = M($name);
        if($_POST['subflag']){
            $m->create();
            $result = $m->add();
            if($result) {
                $this->success('操作成功！',"index");
                exit(0);
            }else{
                $this->error('写入错误！',"index");
                exit(0);
            }
        }else{
            $sql = 'select a1.id,a1.name,a2.name pname from '.getTableName($name).' a1 left join '.getTableName($name).' a2 on a1.pid=a2.id order by a1.pid';
            $pcategory = $m->query($sql);
            $this->assign('pcategory', $pcategory); 
            $this->display();
        }
    }
	
}